Subject: Re: Beginner Question : Filling Arrays from Text Files
Date: Mon, 22 Jan 1996 08:59:14 GMT
Organization: On-Ramp; Individual Internet Connections; Dallas/Ft Worth/Houston, TX USA
Message-ID: <4dvdo1$kg6@news.onramp.net>
References: <1f7cc$13361c.91@news.hampshire.edu>
NNTP-Posting-Host: stemmons15.onramp.net
X-Newsreader: Forte Free Agent 1.0.82
<sbfF94@hamp.hampshire.edu> wrote:
>Hi, we're writing a cgi program in c to produce a multiple-choice quiz on a web page. The program currently works, but we need to convert the contents of the question and answer arrays from being defined within the code:
>to being filled by an external text file with an indeterminate (and changable) number of entries. We are trying to figure out how to use fread() to do this, but are totally confused as to how. We need to know : a)the required format of the text file to make the entire file readable into the array b) how to get the array "question" to fill itself with as many pointers/strings as are in the text file and c)use the number of entries in the text file to constrain rand().
> Also, how does sizeof() work with fread, and do we need to use it?
>we are compiling on unix using gcc.
> Thanks,
> Shana Furman
>
Jon Seag
I would use fgets to find out how many line are in the text file(then
rewind()).
char **question;
qusetion = (char**)malloc(num_lines);
qusetion[0] = (char*)malloc(strlen(str)+1);
or *(question+index) = (char*)malloc(strlen(str)+1);
.
.
Use a dynamic array for question allocating sterlen + 1 for every line
read out of the file(using fgets again) into a buffer(str) as big as
the biggest possable entry. Then just copy the buffer into the pointer